﻿using Consul;
using LDM.Framework.ExceptionHandling.Extensions;
using LDM.Sys.Repository.Interfaces;
using LDM.Sys.Repository.Repository;
using LDM.Sys.Service.Controllers.Base;
using LDM.Sys.ServiceAPI.Model.DBModel;
using LDM.Sys.ServiceAPI.Model.ViewModel.Request.AuthorityReq;
using LDM.Sys.ServiceAPI.Model.ViewModel.Response.Authority;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Model.Models;
using NetTaste;
using System.Linq;
using User.Repositories.Repository.Interfaces;
using Utility.Extensions;
using Utility.Webs.Clients;

namespace LDM.Sys.Service.Controllers
{
    /// <summary>
    /// 用户角色服务
    /// </summary>
    public class SysRoleController : BaseController
    {
        private readonly ISysRoleRepository _sysRoleRepository;
        private readonly ISysRoleAuthorityRelRepository _sysRoleAuthorityRelRepository;
        private readonly ISysUserRoleRelRepository _sysUserRoleRelRepository;
        private readonly ISysAccountRepository _sysAccountRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        public SysRoleController(ISysRoleRepository sysRoleRepository,
            ISysRoleAuthorityRelRepository sysRoleAuthorityRelRepository,
            ISysUserRoleRelRepository sysUserRoleRelRepository,
            ISysAccountRepository sysAccountRepository)
        {
            _sysRoleRepository = sysRoleRepository;
            _sysRoleAuthorityRelRepository = sysRoleAuthorityRelRepository;
            _sysUserRoleRelRepository = sysUserRoleRelRepository;
            _sysAccountRepository = sysAccountRepository;
        }


        #region 基础逻辑
        /// <summary>
        /// 获取角色列表
        /// </summary>
        [HttpPost]
        public PageResult<RoleRes> GetSysRoles(GetSysRoleReq request)
        {
            request.ThrowIfNull();
            PageResult<SysRole> pageRoles = _sysRoleRepository.GetSysRoles(request);
            List<RoleRes> roles = pageRoles.Data.CopyList<SysRole, RoleRes>();
            var roleIds = roles.Select(x => x.Id).ToList();
            if (roleIds.Any())
            {
                var userRoleRels = _sysUserRoleRelRepository.GetRoleUsers(roleIds);
                var briefAccounts = _sysAccountRepository.BriefAccounts();
                foreach (var role in roles)
                {
                    var userIds = userRoleRels.Where(x => x.RoleId == role.Id).Select(x => x.UserId).ToList();
                    role.RoleBriefAccounts = briefAccounts.Where(x => userIds.Contains(x.Id)).ToList();
                }
            }
            return PageResult<RoleRes>.CreateInstance(roles, pageRoles.Total);
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        [HttpPost]
        public void AddSysRole(AddSysRoleReq request)
        {
            request.ThrowIfNull();
            var obj = request.Copy<AddSysRoleReq, SysRole>();
            _sysRoleRepository.Insert(obj);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        [HttpPost]
        public void UpdateSysRole(UpdateSysRoleReq request)
        {
            request.ThrowIfNull();
            var getKey = _sysRoleRepository.GetByKey(request.Id);
            getKey.ThrowIfNull();
            var obj = request.Copy<UpdateSysRoleReq, SysRole>();
            _sysRoleRepository.Update(obj);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        [HttpPost]
        public void DelSysRole(DelSysRoleReq request)
        {
            request.ThrowIfNull();
            var getKey = _sysRoleRepository.GetByKey(request.Id);
            getKey.ThrowIfNull();
            _sysRoleRepository.Delete(request.Id);
        }
        #endregion


        #region 角色和用户

        /// <summary>
        /// 获取角色下的用户信息
        /// </summary>
        [HttpPost]
        public List<int> GetUserByRoleId(int roleId)
        {
            var roleUsers = _sysUserRoleRelRepository.GetRoleUsers(roleId);
            var userIds = roleUsers.Select(x => x.UserId).ToList();
            return userIds;
        }

        /// <summary>
        /// 设置角色下用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public void SetRoleAccounts(SetRoleAccountsReq request)
        {
            request.ThrowIfNull()
                .ThrowIf(x => x.RoleId <= 0, "请输入正确的角色")
                .ThrowIf(x => request.UserId == null || !x.UserId.Any(), "请设置用户信息");
            var role = _sysRoleRepository.GetByKey(request.RoleId);
            role.ThrowIfNull("角色错误");
            var roleUsers = _sysUserRoleRelRepository.GetRoleUsers(request.RoleId);
            if (roleUsers.Any())
            {
                _sysUserRoleRelRepository.BatchDelete(roleUsers);
            }
            var userRoleRels = request.UserId.Select(x => SetRoleAccountsReq.GetUserRoleRels(x, request.RoleId)).ToList();
            _sysUserRoleRelRepository.Insert(userRoleRels);
        }
        #endregion
    }
}
